package com.sixth.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sixth.core.entity.CareOrderItem;
import com.sixth.core.entity.CheckItem;
import com.sixth.core.entity.CheckResult;
import com.sixth.core.util.OSSUtil;
import com.sixth.core.vo.R;
import com.sixth.service.CheckItemService;
import com.sixth.service.CheckResultService;
import com.sixth.vo.CareOrderItemVo;
import com.sixth.vo.CheckResultVo;
import com.sixth.vo.ResultEntryVo;
import com.sixth.vo.UpdateResultEntryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @program: sixth-hospital-parent
 * @description:
 * @author: 李劲锋
 * @create: 2024-08-22 10:50
 **/
@RestController
@RequestMapping("/check/resultEntry")
@Api(tags = "检查结果")
public class CheckResultController {

    @Autowired
    private CheckResultService checkResultService;

    @PostMapping("/listByPage")
    @ApiOperation(value = "检查结果列表")
    public R<IPage<CheckResult>> list(@RequestBody ResultEntryVo resultEntryVo) {
        return checkResultService.selectByPage(resultEntryVo);
    }
    @PostMapping("/listByPageResult")
    @ApiOperation(value = "检查结果分页")
    public R<IPage<CheckResult>> listByPageResult(@RequestBody ResultEntryVo resultEntryVo) {
        return checkResultService.listByPageResult(resultEntryVo);
    }

    // 文件上传接口
    @PostMapping("/doUploadImage")
    @ApiOperation(value = "文件上传")
    public R<String> uploadFile(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return R.error("文件不能为空");
        }
        try {
            String fileUrl = OSSUtil.uploadOss(file);
            if (!"失败".equals(fileUrl)) {
                return R.ok(fileUrl);
            } else {
                return R.error("文件上传失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("文件上传过程中发生错误：" + e.getMessage());
        }
    }

    // 新增文件删除接口
    @DeleteMapping("/deleteImage")
    @ApiOperation(value = "删除文件")
    public R<String> deleteImage(@RequestParam("url") String url) {
        // 从 URL 中提取文件名
        String fileName = url.substring(url.lastIndexOf("/") + 1);

        String endpoint = "oss-cn-beijing.aliyuncs.com";
        String bucketName = "zql-01";
        String accessKeyId = "LTAI5t6FNgQNxZ48iYU2MfmM";
        String secretAccessKey = "eWSKxaTpDJJyt7taI1SJXrmOB1LONm";

        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, secretAccessKey);
        try {
            ossClient.deleteObject(bucketName, fileName);
            return R.ok("文件删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("文件删除失败");
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
    @PutMapping("/complete")
    @ApiOperation(value = "更新检查结果完成")
    public R<CheckResult> startCheckUpdate(@RequestBody UpdateResultEntryVo updateResultEntryVo) {
        return checkResultService.startCheckUpdate(updateResultEntryVo);
    }


}
